// url模块，用于解析处理url地址

// 加载模块
let url = require('url');

// url.resolve(path1,path2)用于拼接url地址

// 后面的路径替换前面最后部分的路径
// console.log( url.resolve('http://localhost:3000/a','search/images/01.png') );
// http://localhost:3000/search/images/01.png

// 后面的路径替换前面最后部分的路径
// console.log( url.resolve('http://localhost:3000/a/','search/images/01.png') );
// http://localhost:3000/a/search/images/01.png

// 后面的路径替换端口号后面所有的路径
// console.log( url.resolve('http://localhost:3000/a/b','/search/images/01.png') );
// http://localhost:3000/search/images/01.png

// console.log( url.resolve('http://localhost:3000/','search/images/01.png') );
// http://localhost:3000/search/images/01.png


// url.parse(urlStr)方法用于将url字符串解析为url对象
// let urlStr = 'http://localhost:3000/search/images/01.png?hehe=123&haha=456#head';
// let urlObj = url.parse(urlStr);// 第二个参数默认为false，query是字符串
// let urlObj = url.parse(urlStr,true);// 第二个参数为true，query是对象
// console.log( urlObj );
// console.log( urlObj.query.hehe );// '123'
// urlObj = {
//     protocol: 'http:',
//     slashes: true,
//     auth: null,
//     host: 'localhost:3000',
//     port: '3000',
//     hostname: 'localhost',
//     hash: '#head',
//     search: '?hehe=123&haha=456',
//     query: 'hehe=123&haha=456',
//     query: { hehe: '123', haha: '456' },
//     pathname: '/search/images/01.png',
//     path: '/search/images/01.png?hehe=123&haha=456',
//     href: 'http://localhost:3000/search/images/01.png?hehe=123&haha=456#head'
// }


// 从nodejs 11之后，官方推荐实用URL类来操作url
let urlObj2 = new URL('http://localhost:3000/search/images/01.png?hehe=123&haha=456#head');
console.log( urlObj2 );
console.log( urlObj2.searchParams.get('hehe') );// '123'
// urlObj2 = {
//     href: 'http://localhost:3000/search/images/01.png?hehe=123&haha=456#head',
//     origin: 'http://localhost:3000',
//     protocol: 'http:',
//     username: '',
//     password: '',
//     host: 'localhost:3000',
//     hostname: 'localhost',
//     port: '3000',
//     pathname: '/search/images/01.png',
//     search: '?hehe=123&haha=456',
//     searchParams: URLSearchParams { 'hehe' => '123', 'haha' => '456' },
//     hash: '#head'
// }